﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using WL_Store.BLL;
using WL_Store.Common;
using WL_Store.Model;

namespace WL_Store.Web.ashx
{
    /// <summary>
    /// Login 的摘要说明
    /// </summary>
    public class Login : IHttpHandler, IRequiresSessionState
    {
        OperateResult result = new OperateResult();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string name = context.Request.Form["username"];
            string password = context.Request.Form["userpwd"];
            string captcha = context.Request.Form["Codes_text"];
            string code = context.Session["Code"].ToString();
            if (captcha.ToUpper() == code.ToUpper())
            {

                EmployeeInfoBLL bll = new EmployeeInfoBLL();
                //LoginBLL lb = new LoginBLL();
                EmployeeInfoModel Log = bll.EmployeeInfo(name, password);
                if (Log != null)
                {
                   
                    context.Session["EmployeeInfo"] = Log;
                    AddLoginLogs();
                    result.State = 1;
                    result.Message = "登录成功！！！";

                }
                else
                {
                    result.State = -1;
                    result.Message = "用户名或密码错误！！！";
                    //返回json格式的字符串
                    //context.Response.Write("{State:-1,Message:'用户名或密码错误！'}");
                }
            }
            else
            {
                result.State = 0;
                result.Message = "验证码错误！！！";
                //context.Response.Write("{State:false,Message:'验证码错误！'}");
            }

            //将对象转化为json格式的字符串（序列化）
            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(result);
            //返回json格式的字符串
            context.Response.Write(json);



        }

        /// <summary>
        /// 添加登录日志
        /// </summary>
        private void AddLoginLogs()
        {
            LoginLogsBLL bll = new LoginLogsBLL();
            //获取页面登录名
            string LoginName =HttpContext.Current.Request.Form["username"];
            //获取用户姓名
            string AdminName = bll.GetName(LoginName).ToString();
            //获取当前计算机名
            string ServerName = Dns.GetHostName();
            //获取本地的IP地址
            string AddressIP = string.Empty;
            foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    AddressIP = _IPAddress.ToString();
                }
            }
            //获取当前时间
            DateTime dateTime = DateTime.Now;

            LoginLogsModel model = new LoginLogsModel()
            {
                LoginName = LoginName,
                AdminName = AdminName,
                ServerName = ServerName,
                ClientIP = AddressIP,
                LoginTime = dateTime
            };
            bll.AddLonginLog(model);

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}